angular.module('Controller')

.controller 'EdNavCtrl', ($scope, $rootScope,commonUtil,menuManage,authorityManage, $http) ->
    $scope.taskRemainingCount = 0
    loadMenu=()->
#        $scope.menus=menuManage.get()
        $http.get('@ostengar/adminUser/module')
        .success((resp)->
            if resp.code==0
                list=[]
                _.each resp.message,(item)->
                    menu={
                        id:item.id
                        name:item.moduleName
                        href:item.moduleUrl
                        icon:item.moduleIcon
                        subMenus:[]
                        isLeaf:item.isLeaf #item.subModules.length==0 ? true:false
                    }
                    if item.subModules and item.subModules.length>0
                        _.each item.subModules,(subItem)->
                            sub={
                                id:subItem.id
                                name:subItem.moduleName
                                href:subItem.moduleUrl
                                icon:subItem.moduleIcon
                                isLeaf:subItem.isLeaf
                            }
                            menu.subMenus.push sub
                    list.push menu
                $scope.menus=list
                $rootScope.$broadcast('menu-loaded',angular.copy($scope.menus))
        )
    loadAuthority=()->
        $http.get('@ostengar/adminUser/authority')
        .success((resp)->
            if resp.code==0
                loadResource(resp.message)
        )
    loadResource=(authoritys)->
        requestData = {
            page: 0,
            size: 999,
        }
        getParam = commonUtil.transferGetParam(requestData)
        list=[]
        _.each authoritys,(item)->
            $http.get('@ostengar/authority/'+item.id+'/resources'+getParam)
            .success((resp)->
                if(resp.code==0)
                    authorityManage.union(resp.message.content)
            )
    loadAuthority()
    loadMenu()
    calPageHeight=()->
        pageHeight=$('.nav-container').get(0)?.offsetHeight
        if pageHeight
            tabHgt=$('#myTab').get(0).offsetHeight
            $('#wf-main-container').css('height',(pageHeight+60)+'px')
            $('#myTabContent').css('height',(pageHeight-tabHgt)+'px')
        ''
    _.defer(()->
        calPageHeight()
        $(window).resize(()->
            calPageHeight()
        )
    )
    $scope.clickMenu=(event,item)->
        if event
            event.preventDefault()
        if item.isLeaf==false
            spreadMenu(item)
        else
            goPage(item)
            document.documentElement.scrollTop = document.body.scrollTop =0
            ''
        return true
    spreadMenu=(item)->
        menuId=item.id
        if($('#'+menuId).hasClass('open'))
            $('#'+menuId).removeClass("open").find("ul").slideUp();
        else
            $('.nav-menu').removeClass("open").find("ul").slideUp();
            $('#'+menuId).addClass("open").find("ul").slideDown();
    goPage=(item)->
        menuManage.goPage(item)